package com.fpswork.core.exception.exception;



public class BusinessException extends  Exception {
	public String errorNo=null;
	public String message1=null;
	public String message2=null;
	public String message3=null;
	public String message4=null;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public BusinessException(){super();}
	public BusinessException(String message){super(message);}
	
	public static BusinessException  getBusinessException(String errorNo,String message){
		BusinessException businessException = new BusinessException(message);
		businessException.setErrorNo(errorNo);
		return businessException;
	}
	
	
	
	public BusinessException(Throwable cause){
		super(cause);
		String msg=cause.getMessage();
		if(msg==null){msg="";}
		
 
		
		if(msg.indexOf("ORA-")>=0&&msg.indexOf(":")>=0){
			if(msg.indexOf("ORA-")>=0&&msg.indexOf(":")>=0){
				msg=msg.substring(msg.indexOf("ORA-"));
				msg=msg.substring(msg.indexOf(":")+1);
				msg=msg.trim();
				if(msg.indexOf("ORA")==0&&msg.indexOf("::")>=0){
				 	
					String temp = msg.substring(0,  msg.indexOf("::"));
					if(temp!=null&&!"".equals(temp.trim())){
						errorNo=temp;
					}
					
				}
			}
		}
		
		
		if(cause instanceof BusinessException){
			errorNo=((BusinessException)cause).errorNo;
		}
 
	}
	
	
	public static BusinessException  getBusinessExceptionI18n(String msg,String msg1,String msg2,String msg3,String msg4){
		BusinessException businessException = new BusinessException(msg);
		businessException.message1=msg1;
		businessException.message2=msg2;
		businessException.message3=msg3;
		businessException.message4=msg4;
		return businessException;
	}
	
 	
	
	public static BusinessException  getBusinessException(String errorNo,Throwable cause){
		BusinessException businessException = new BusinessException(cause);
		businessException.setErrorNo(errorNo);
		return businessException;
	}
	

	
	public BusinessException(String message, Throwable cause){super(message,cause);
		if(cause instanceof BusinessException){
			errorNo=((BusinessException)cause).errorNo;
		}
	}
	
	 	
	
	public static BusinessException  getBusinessException(String errorNo,String message, Throwable cause){
		BusinessException businessException = new BusinessException(message,cause);
		businessException.setErrorNo(errorNo);
		return businessException;
	}
	
	public String getMessage(){
		String msg=super.getMessage();
		if(msg==null){msg="";}
		if(msg.indexOf("ORA-")>=0&&msg.indexOf(":")>=0){
				if(msg.indexOf("ORA-")>=0&&msg.indexOf(":")>=0){
					msg=msg.substring(msg.indexOf("ORA-"));
					msg=msg.substring(msg.indexOf(":")+1);
					msg=msg.trim();
					if(msg.indexOf("ORA")==0&&msg.indexOf("::")>=0){
						msg=msg.substring(msg.indexOf("::")+2);
						if(msg.indexOf("ORA-")>=0){
							msg=msg.substring(0,msg.indexOf("ORA-"));
						}
						
					}
				}
		}
		return msg; 
	}
	
	
	public String getMessage(String type){
		String msg="";
		if("1".equals(type)){
			msg=this.message1;
		}else if("2".equals(type)){
			msg=this.message2;
		}else if("3".equals(type)){
			msg=this.message3;
		}else if("4".equals(type)){
			msg=this.message4;
		} 
		if(msg!=null&&!"".equals(msg.trim())){
			return msg;
		}
 
		msg=super.getMessage();
		if(msg==null){msg="";}
		if(msg.indexOf("ORA-")>=0&&msg.indexOf(":")>=0){
				if(msg.indexOf("ORA-")>=0&&msg.indexOf(":")>=0){
					msg=msg.substring(msg.indexOf("ORA-"));
					msg=msg.substring(msg.indexOf(":")+1);
					msg=msg.trim();
					if(msg.indexOf("ORA")==0&&msg.indexOf("::")>=0){
						msg=msg.substring(msg.indexOf("::")+2);
						
						if(msg.indexOf("ORA-")>=0){
							msg=msg.substring(0,msg.indexOf("ORA-"));
						}						
						
					}
				}
		}
		if(errorNo==null||"".equals(errorNo)){
			return msg;
		}else{
			 
			return msg;
		}
	}
	public void setErrorNo(String errorNo) {
		this.errorNo = errorNo;
	}
	
	
}
